/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.central.common.exception;

import com.central.common.utils.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 异常处理器
 *
 */
@Slf4j
@RestControllerAdvice
@AllArgsConstructor
public class BusinessExceptionHandler {
    /**
     * 处理自定义异常
     */
    @ExceptionHandler(BusinessException.class)
    public Result handleRenException(BusinessException ex) {
        return Result.failedWith(null, ex.getCode(), ex.getMessage());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public Result handleDuplicateKeyException(DuplicateKeyException ex) {
        return Result.failed(BizExceptionEnum.DB_RECORD_EXISTS);
    }

    @ExceptionHandler(UnauthorizedException.class)
    public Result handleDuplicateKeyException(UnauthorizedException ex) {
        return Result.failed(BizExceptionEnum.UNAUTHORIZED);
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception ex) {
        ex.printStackTrace();
        return Result.failed(BizExceptionEnum.SERVER_ERROR);
    }
}